package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.object.pojo.MachineNeatLog;
import com.tbit.common.entity.pojo.vo.DateResult;
import com.tbit.uqbike.object.pojo.statistics.ExportMachineCleanLog;
import com.tbit.uqbike.object.pojo.statistics.ExportPowerLog;
import com.tbit.uqbike.object.value.Page;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.MachineNeatLogService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.PageHelperUtil;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelUtil;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelWrapper;
import com.tbit.utils.DateTimeUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 摆车日志(MachineNeatLog)表控制层
 *
 * @author jing
 * @since 2021-01-11 15:37:55
 */
@RestController
@RequestMapping("/machineneatlog")
public class MachineNeatLogController {
    /**
     * 服务对象
     */
    @Resource
    private MachineNeatLogService machineNeatLogService;

    /**
     * 通过主键查询单条数据
     *
     * @param machineNeatLogId 主键
     * @return 单条数据
     */
    @PostMapping("/selectList")
    public JsonResult selectList(Integer machineNeatLogId) {
        return JsonResult.ok(this.machineNeatLogService.queryById(machineNeatLogId));
    }

    /**
     * 统计每天清洁车总数
     */
    @PostMapping("/statisticsByTime")
    public JsonResult statisticsByTime(MachineNeatLog machineNeatLog) {
        Assert.notNull(machineNeatLog.getAccountId(), "ver.areaId.notNull");
        Assert.notNull(machineNeatLog.getEndTime(), "sugar.endRideDt.notNull");
        Assert.notNull(machineNeatLog.getStartTime(), "sugar.startRideDt.notNull");
        List<DateResult> list = machineNeatLogService.statisticsByTime(machineNeatLog);
        return JsonResult.ok(list);
    }

    /**
     * 统计详情
     */
    @PostMapping("/queryPage")
    public JsonResult queryPage(MachineNeatLog machineNeatLog) {
        Assert.notNull(machineNeatLog.getAccountId(), "ver.areaId.notNull");
        Assert.notNull(machineNeatLog.getEndTime(), "sugar.endRideDt.notNull");
        Assert.notNull(machineNeatLog.getStartTime(), "sugar.startRideDt.notNull");
        machineNeatLog.setPage(new Page(machineNeatLog.getPageNO(), machineNeatLog.getRowCount()));
        PageHelperUtil.initPage(machineNeatLog.getPage());
        List<MachineNeatLog> machineNeatLogs = machineNeatLogService.queryPage(machineNeatLog);
        PageInfo<MachineNeatLog> pageInfo = new PageInfo<>(machineNeatLogs);
        return JsonResult.ok(pageInfo.getList(), (int) pageInfo.getTotal());
    }

    @GetMapping("/exportByTime")
    public void exportByTime(MachineNeatLog machineNeatLog, HttpServletResponse response) {
        Assert.notNull(machineNeatLog.getAccountId(), "ver.areaId.notNull");
        Assert.notNull(machineNeatLog.getEndTime(), "sugar.endRideDt.notNull");
        Assert.notNull(machineNeatLog.getStartTime(), "sugar.startRideDt.notNull");
//        if(null==machineNeatLog.getAccountUserId()){
//            throw new BaseException(SystemCodeEnum.SELECT_OPERATION_AND_MAINTENANCE_PERSONNEL);
//        }
        List<ExportMachineCleanLog> exports = machineNeatLogService.exportByTime(machineNeatLog);
        ExportExcelWrapper<ExportMachineCleanLog> export = new ExportExcelWrapper();
        String filename = "摆车统计" + DateTimeUtil.getNowTimeYMD();
        String[] columnNames = {"运维名称", "车辆编号", "开始时间", "结束时间", "是否有效"};
        export.exportExcel(filename, filename, columnNames, exports, response, ExportExcelUtil.EXCEL_FILE_2003);
    }

    /**
     * 统计每个运维人员总数
     */
    @PostMapping("/getByType")
    public JsonResult getById(MachineNeatLog machineNeatLog) {
        Assert.notNull(machineNeatLog.getAccountId(), "ver.areaId.notNull");
        Assert.notNull(machineNeatLog.getEndTime(), "sugar.endRideDt.notNull");
        Assert.notNull(machineNeatLog.getStartTime(), "sugar.startRideDt.notNull");
        machineNeatLog.setPage(new Page(machineNeatLog.getPageNO(), machineNeatLog.getRowCount()));
        PageHelperUtil.initPage(machineNeatLog.getPage());
        List<ExportPowerLog> list = machineNeatLogService.getById(machineNeatLog);
        PageInfo<ExportPowerLog> pageInfo = new PageInfo<>(list);
        return JsonResult.ok(pageInfo.getList(), (int) pageInfo.getTotal());
    }

}